;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.Outbound", [])
        .controller("OutboundCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            $scope.yesOrNo = [
                {key: "1", value: "是"},
                {key: "0", value: "否"}
            ];
            $scope.deliveryStatues = [];
            $scope.btnPermission = {
                saleOrderExport: true,
                saleOrderRemoveCache: true
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.outState = [
                {id: 0, name: "未出库"},
                {id: 1, name: "部分出库"},
                {id: 2, name: "已出库"},
                {id: 3, name: "强制结单"}
            ];

            $scope.billState = [
                {id: 1, name: "普通"},
                {id: 2, name: "三包"},
                {id: 3, name: "赠送"}
            ];

            $scope.giveQuotaTypes = [
                {id: 1, name: "通用"},
                {id: 2, name: "保养"}
            ];

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/report/exportSalesOutbound")
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.tbConf = {
                scrollWidth: 1210,
                tableCol: [
                    {
                        name: "销售单号",
                        dataField: "saleNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "销售出库单号",
                        dataField: "saleOutNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "借用单号",
                        dataField: "borrowNo",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "单据类型",
                        dataField: "billTypeStr",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "制单时间",
                        dataField: "makeTimeStr",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "制单人",
                        dataField: "makeName",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "客户名称",
                        dataField: "clientName",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "服务人员",
                        dataField: "servicePersonName",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "出库时间",
                        dataField: "outboundTimeStr",
                        width: 170,
                        isShow: true
                    },
                    {
                        name: "出库状态",
                        dataField: "outboundStatusStr",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "出库地点",
                        dataField: "outboundLocation",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "价格方案",
                        dataField: "pricePlan",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件分类",
                        dataField: "materialsCategory",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "零件号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 180,
                        isShow: true
                    },
                    {
                        name: "申请数量",
                        dataField: "applyQty",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "单位",
                        dataField: "unit",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "原单价",
                        dataField: "originalPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "原总价",
                        dataField: "originalTotalPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "调整后单价",
                        dataField: "adjustPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "调整后总价",
                        dataField: "adjustTotalPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "本次发货数量",
                        dataField: "thisSendQty",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "实际出库数量",
                        dataField: "actualOutboundQty",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "出库金额",
                        dataField: "outboundAmount",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "成本单价",
                        dataField: "costPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "成本小计",
                        dataField: "costSubtotal",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "订单金额",
                        dataField: "orderAmount",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "赠送类型",
                        dataField: "giveQuotaTypeDesc",
                        width: 100,
                        isShow: true
                    }
                ]
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                var reqUrl = "/report/salesOutboundReportList";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition).forEach(function (key) {
                    if (!$scope.condition[key] && $scope.condition[key] !== 0) delete $scope.condition[key];
                });
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.makeTimeStr = $filter("YbDate")(item.makeTime, "yyyy-MM-dd HH:mm:ss");
                        item.outboundTimeStr = $filter("YbDate")(item.outboundTime, "yyyy-MM-dd HH:mm:ss");
                        item.outboundStatusStr = $filter("ProgressStatus")(item.outboundStatus, "outDepot");
                        item.billTypeStr = $filter("BillTypeStatus")(item.billType);
                        item.giveQuotaTypeDesc = $filter("GiveQuotaTypes")(item.giveQuotaType);
                    });
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                });
            }

            var btnPermissionCode = {
                saleOutboundExport: window.ActionCode.saleOutboundExport
            };

            $scope.btnPermission = {
                saleOutboundExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);